#ifndef DBASEPRX_H
#define DBASEPRX_H

#include <os.h>
#include <string>
#include <iostream>
#include <sstream>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <mysql/mysql.h> // sudo apt-get install libmysqlclient-dev

//-----------------------------------------------------------------------------
using namespace std;

//-----------------------------------------------------------------------------
class dbresult
{
    MYSQL_RES* _prez;
public:
    dbresult(MYSQL_RES* p):_prez(p){}
    dbresult():_prez(0){}
    ~dbresult()
    {
        if(_prez)
            mysql_free_result(_prez);
    }

    inline uint32_t rows()
    {
        if(_prez)
            return  mysql_num_rows(_prez);
        return 0;
    }

    MYSQL_ROW fetch()
    {
        static char* S[]={_CC("0"),};
        if(_prez)
            return mysql_fetch_row(_prez);
        return S;
    }
    char* fetch(int index)
    {
        static char SS[]="0";
        if(_prez)
            return mysql_fetch_row(_prez)[index];
        return SS;
    }
};

//-----------------------------------------------------------------------------
class SR
{

public:
    SR(dbresult* pr):_pr(pr){}
    ~SR(){ if(_pr) delete _pr;}
    dbresult* operator->(){
        static dbresult sr;
        return _pr  ? _pr : & sr;
    }
    SR(SR& r):_pr(r._pr) {r._pr=0;};
    void detach()
    {
        if(_pr)
            delete _pr;
        _pr = 0;
    }

    SR& operator=(dbresult* pr){
        delete _pr;
        _pr = pr;
        return *this;
    }
private:

    SR& operator=(const SR& r);
    dbresult* _pr;
};

//---------------------------------------------------------------------------------------
class mysqlcls
{
    bool _connected;
public:
    mysqlcls():_connected(false)
    {

    }

    virtual ~mysqlcls()
    {
        disconnect();
    }

    inline my_ulonglong lastid()
    {
        return mysql_insert_id(&_mysql);
    }

    bool     connect(const char* hostname, const char* username, const char* password)
    {
        mysql_init(&_mysql); // link with -lmysqlclient

        mysql_options(&_mysql,MYSQL_READ_DEFAULT_GROUP,"mitza");
        mysql_options(&_mysql,MYSQL_OPT_COMPRESS,0);
        if (!mysql_real_connect(&_mysql,hostname,username,password,"",0,NULL,0))
        {
            error(); //install: mysql-server-5.5 mysqlclient-dev
            return 0;
        }
        _connected=true;
        qry("use mitza;");
        return 1;

    }
    void    disconnect()
    {
        if(_connected)
            mysql_close(&_mysql);
        _connected=false;
    }

    void error()
    {
        printf("ERROR: (%s)\r\n",mysql_error(&_mysql));
        printf("%s\r\n",_lastq.c_str());

        _err=1;
    }
    int err()const{return _err;}
    dbresult* qry(const stringstream& s, bool ret=false){return qry(s.str().c_str(), ret);}
    dbresult* qry(const string& s, bool ret=false){return qry(s.c_str(), ret);}
    dbresult* qry(const char* q, bool ret=false)
    {
        _err=0;

        _lastq = q;

        int r =  mysql_query(&_mysql, q);
        if(0==r)
        {
            if(ret)
                return new dbresult(mysql_store_result(&_mysql));
            return 0;
        }
        error();
        return 0;
    }

    void use_db (const char* s)
    {

        _lastq = s;

        mysql_select_db(&_mysql,s);
    }
    bool createtables();
    uint32_t   sess_id();
public:
    MYSQL       _mysql;

    string      _lastq;

    int         _err;
};



#endif // DBASEPRX_H
